package com.leapp.sk.viewdelegate.login

import android.support.v7.app.AppCompatActivity
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKDeviceUtil
import cc.vv.lkbasecomponent.util.LKRegexUtil
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.example.util.IntegerUtil
import com.leapp.sk.R
import com.leapp.sk.ui.activity.login.LoginActivity

/**登录视图托管层
 * @author  chaohaipeng  on 2018/7/10.
 */
class LoginDelegate: BaseAppDelegate(){
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_login
    }

    /**
     * 验证码和密码登录设置逻辑
     */
   fun passwordAndCodeLogin(isCodeLogin:Boolean){
        val rl_al_code= getView<RelativeLayout>(R.id.rl_al_code)
        val rl_al_password= getView<RelativeLayout>(R.id.rl_al_password)
        val rl_al_forget_passwrod= getView<RelativeLayout>(R.id.rl_al_forget_passwrod)
        val tv_al_password_and_code= getView<TextView>(R.id.tv_al_password_and_code)
        if (isCodeLogin){
            rl_al_code.visibility= View.VISIBLE
            rl_al_password.visibility= View.GONE
            rl_al_forget_passwrod.visibility= View.GONE
            tv_al_password_and_code.text=getActivity<AppCompatActivity>().resources.getString(R.string.string_login_password_login)

        }else{
            rl_al_code.visibility= View.GONE
            rl_al_password.visibility= View.VISIBLE
            rl_al_forget_passwrod.visibility= View.VISIBLE
            tv_al_password_and_code.text=getActivity<AppCompatActivity>().resources.getString(R.string.string_login_code_login)
        }
   }

    /**
     * 设置清理按钮隐显示
     */
    fun setClearDatavisible(){

        val iv_al_user_agressment= getView<ImageView>(R.id.iv_al_user_agressment)
        iv_al_user_agressment.isSelected=true
        val iv_al_clear_content= getView<ImageView>(R.id.iv_al_clear_content)
        val et_al_phone= getView<ContainsEmojiEditText>(R.id.et_al_phone)
        et_al_phone.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                    if(compareTo==0){
                        iv_al_clear_content.visibility=View.INVISIBLE
                    }else{
                        iv_al_clear_content.visibility=View.VISIBLE
                    }
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
    }
    /**
     * 清理内容
     */
    fun clearContent(){
        val et_al_phone= getView<ContainsEmojiEditText>(R.id.et_al_phone)
        et_al_phone.setText("")
    }
    /**
     * 设置账号
     */
    fun setPhone(phone:String){
        val et_al_phone= getView<ContainsEmojiEditText>(R.id.et_al_phone)
        if (!TextUtils.isEmpty(phone)){
            et_al_phone.setText(phone)
        }

    }

    /**
     * 验证码和密码登录设置逻辑
     */
    fun passwordVisible(isVisible:Boolean){
        val rl_al_code= getView<ImageView>(R.id.iv_al_password_visible)
        val et_al_password= getView<ContainsEmojiEditText>(R.id.et_al_password)
        if (isVisible){
            et_al_password.isSelected=true
            et_al_password.transformationMethod=HideReturnsTransformationMethod.getInstance()
        }else{
            et_al_password.isSelected=false
            et_al_password.transformationMethod= PasswordTransformationMethod.getInstance()
        }
    }

    /**
     * 登录校验
     */
   fun loginCheck():String{
       val et_al_code= getView<ContainsEmojiEditText>(R.id.et_al_code)
       val codeV=et_al_code.text.toString().trim()
       if (IntegerUtil.getInstance().isInteger(codeV)&&codeV.length==6){
           return codeV
       }else{
           LKToastUtil.showToastLong(getActivity<LoginActivity>().resources.getString(R.string.string_login_toast_check_code))
           return ""
       }

   }
    /**
     * 手机号校验
     */
    fun verificationCodeCheck():String{
        val et_al_phone= getView<ContainsEmojiEditText>(R.id.et_al_phone)
        val tel=et_al_phone.text.toString().trim()
        if (LKRegexUtil.isMobileExact(tel)){
            return tel
        }else{
            LKToastUtil.showToastLong(getActivity<AppCompatActivity>().resources.getString(R.string.string_login_check_phone))
            return ""
        }
    }
    /**
     * 手机号校验
     */
    fun setverificationCode(code:String?){
        val et_al_code= getView<ContainsEmojiEditText>(R.id.et_al_code)
        if (!TextUtils.isEmpty(code)){
            et_al_code.setText(code)
        }
    }
    /**
     * 验证码
     */
  fun verificationCode(millisUntilFinished:Long){
        val tv_al_get_code= getView<TextView>(R.id.tv_al_get_code)
    if (millisUntilFinished>0){
        tv_al_get_code.isClickable=false
        tv_al_get_code.text=(millisUntilFinished / 1000).toString() + " s"
    }else{
        tv_al_get_code.isClickable=true
        tv_al_get_code.text=getActivity<LoginActivity>().resources.getString(R.string.string_login_again_send)
    }

  }




}